package study.ai.langchain4j.controller;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.service.MemoryId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.ai.langchain4j.service.ChatAssistant;

@Slf4j
@RestController
@RequestMapping("/memory")
public class MemoryController {

    private ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);

    @Autowired
    private ChatLanguageModel chatLanguageModel;
    @Autowired
    private ChatAssistant chatAssistant;

    @RequestMapping("/chat/low")
    public String chatByLow(String message) {

        //
        chatMemory.add(UserMessage.from(message));
        // memory 全部 message
        ChatResponse response = chatLanguageModel.chat(chatMemory.messages());
        //
        chatMemory.add(response.aiMessage());
        return response.aiMessage().text();
    }

    @RequestMapping("/chat/high")
    public String chatByHigh(String memoryId, String message) {
        //
        return chatAssistant.chat(memoryId, message);
    }
}
